#include "key_device.h"

const uint8_t u8Key_Map[] = 
{
    // 单个按键
    KEY_VAL_SUB,
    KEY_VAL_ADD,
    KEY_VAL_OK,
    KEY_VAL_RESET,
    // 组合按键
    KEY_VAL_DEBUG,  
    0x00,
    0x00,
    0x00,
    0x00,

    0x00,

};


void KeyDeviceInit(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOB_CLK_ENABLE(); /* 使能GPIOB时钟 */

    GPIO_InitStruct.Pin = KEY_IO_DOWN | KEY_IO_UP | KEY_IO_OK | KEY_IO_RESET;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;         /* 输入模式 */
    GPIO_InitStruct.Pull = GPIO_PULLUP;           /* 使能上拉 */
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;    /* GPIO速度 */

    /* GPIO初始化 */
    HAL_GPIO_Init(KEY_IO_PORT, &GPIO_InitStruct);

    #if 1   // 调试使用引脚
//    GPIO_InitTypeDef GPIO_InitStruct;

    __HAL_RCC_GPIOA_CLK_ENABLE(); /* 使能GPIOA时钟 */

    GPIO_InitStruct.Pin = GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;         /* 推挽输出模式 */
    GPIO_InitStruct.Pull = GPIO_NOPULL;           /* 关闭上拉 */
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;    /* GPIO速度 */

    /* GPIO初始化 */
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    #endif
}



uint8_t KeyValue = 0;
uint8_t u8Touch_Last_Number = 0;    //上次按键个数
uint8_t u8Touch_Press_Number = 0;   //本次按键个数
uint8_t u8Touch_Value = 0;          //第一个键
uint8_t u8Touch_Shift = 0;          //第二个键

uint8_t Key_Value_out = 0;          //按键值输出



//===按键扫描
void KeyValueScan(void)
{
    uint8_t key_val = 0;
    uint8_t i = 0;

    //获取按键引脚值
    #if 1       // 优化按键查询
    key_val = KEY_IO_PORT->IDR & 0x0F;
    KeyValue = (~key_val) & 0x0F;
    #else
    key_val = HAL_GPIO_ReadPin(KEY_IO_PORT, KEY_IO_DOWN);
    if(key_val == 0)
    {
        KeyValue |= 0x01;
    }
    else
    {
        KeyValue &= ~0x01;
    }

    key_val = HAL_GPIO_ReadPin(KEY_IO_PORT, KEY_IO_UP);
    if(key_val == 0)
    {
        KeyValue |= 0x02;
    }
    else
    {
        KeyValue &= ~0x02;
    }

    key_val = HAL_GPIO_ReadPin(KEY_IO_PORT, KEY_IO_OK);
    if(key_val == 0)
    {
        KeyValue |= 0x04;
    }
    else
    {
        KeyValue &= ~0x04;
    }

    key_val = HAL_GPIO_ReadPin(KEY_IO_PORT, KEY_IO_RESET);
    if(key_val == 0)
    {
        KeyValue |= 0x08;
    }
    else
    {
        KeyValue &= ~0x08;
    }
    #endif
    
    // 按键轮询
    while(KeyValue != 0)
    {
        if(KeyValue & 0x01)
        {
            u8Touch_Press_Number++;
            if (u8Touch_Press_Number == 1)
				u8Touch_Value = i;
			else if (u8Touch_Press_Number == 2)
				u8Touch_Shift = i;
			else
				break;
        }
        i++;
        KeyValue >>= 1;

    }

    switch (u8Touch_Press_Number)
    {
        case 0:
        {
            Key_Value_out = 0;

            break;
        }
        case 1:
        {
            Key_Value_out = u8Key_Map[u8Touch_Value];
            break;
        }
        case 2:
        {
            Key_Value_out = 0xFF;       // 组合键
            if((u8Touch_Value == 0 && u8Touch_Shift == 1) || (u8Touch_Value == 1 && u8Touch_Shift == 0))
            {
                Key_Value_out = KEY_VAL_DEBUG;
            }

            break;
        }

        default:
            break;
    }
    u8Touch_Press_Number = 0;

}





